package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.client.ProductFeignClient;
import com.atguigu.gmall.common.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.FileWriter;
import java.io.IOException;

@Controller
public class IndexController {

    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private TemplateEngine templateEngine;

    @GetMapping({"/", "index.html"})
    public String index(Model model) {
        Result result = productFeignClient.getBaseCategoryList();
        model.addAttribute("list",result.getData());
        return "index/index";
    }

    @GetMapping("createIndex")
    public Result createIndex() throws IOException {
        Result result = productFeignClient.getBaseCategoryList();

        Context context = new Context();
        context.setVariable("list",result.getData());

        FileWriter fileWriter = new FileWriter("D:\\index.html");
        templateEngine.process("index/index.html",context,fileWriter);

        return Result.ok();
    }
}
